package cn.sumpu.app.guoguojie.web.mvc.family;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.guoguojie.biz.AppEventBiz;
import cn.sumpu.app.guoguojie.domain.AppEvent;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.util.Common;

@Controller
public class CalendarController {
	@Autowired
	AppEventBiz eventBiz;
	@RequestMapping(value = "/calendar.htm")
	public String calendar(HttpServletRequest request,
			HttpServletResponse response) {
		return "app/calendar/calendar";
	}
	
	public String hasEvent(String today,HttpServletRequest request,HttpServletResponse response){
		
		return null;
	}
	@RequestMapping(value = "/saveEvent.htm")
	public String saveEvent(AppEvent appEvent,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html; charset=UTF-8");
		Member member = (Member) request.getSession().getAttribute("member");
		appEvent.setMember(member.getIdentifier());
		appEvent.setFamily(member.getFamily());
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			eventBiz.insert(appEvent);
			pw.print("success");
		}catch(Exception e){
			e.printStackTrace();
			pw.print("error");
		}
		return null;
	}
	
	@RequestMapping(value = "/deleteEvent.htm")
	public String deleteEvent(String eventId,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			eventBiz.deleteById(eventId);
			pw.print("success");
		}catch(Exception e){
			e.printStackTrace();
			pw.print("error");
		}
		return null;
	}
	
	@RequestMapping(value = "/eventDayList.htm")
	public String eventDayList(String eventDay,String eventId,HttpServletRequest request,HttpServletResponse response){
		Member member = (Member) request.getSession().getAttribute("member");
		try { 
			List<AppEvent> eventList = eventBiz.getAppEventByDay(member.getFamily(), eventDay);
			request.setAttribute("eventList", eventList);
			request.setAttribute("eventId", eventId);
			request.setAttribute("eventDay", eventDay+" "+"00:00:00");
		}catch(Exception e){
			e.printStackTrace();
		}
		return "app/calendar/eventDayList";
	}
	@RequestMapping(value = "/eventXml.htm")
	public String eventXml(HttpServletRequest request,HttpServletResponse response){
		Member member = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control","no-cache"); 
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			Set<String> setList = new HashSet<String>();
			
			List<AppEvent> eventList = eventBiz.getAppEventByFamily(member.getFamily());
			for (AppEvent appEvent : eventList) {
				String startTime = appEvent.getStartTime();
				if(!Common.empty(startTime))
				setList.add(startTime.substring(0, 10)+" 00:00:00");
			}
			StringBuffer sbXml = new StringBuffer("<data>");
			for (String time : setList) {
				sbXml.append("<event id='event_"+time+"' start_date='"+time+"' end_date='"+time+"'/>");
			} 
			
			sbXml.append("</data>");
			pw.print(sbXml);
		}catch(Exception e){
			e.printStackTrace();
			pw.print("error");
		}
		return null;
	}

}
